package com.jtjrenren.android.taxi.driver.manger.push.cmd;

import com.jtjrenren.android.taxi.driver.bean.control.CmdOtherDutyOnResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdQdResultResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CenterResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdCommonResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdCostResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdPasPayResultResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdQrCkScResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdQrQjCkResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdQzXbResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdXDResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdZxQxDdResponse;
import com.jtjrenren.android.taxi.driver.utils.ModuleUtils;
import com.library.utils.ConvertUtils;
import com.library.utils.LogUtil;

import java.util.List;

/**
 * Created by Administrator on 2016/2/26.
 */
public class ReceiveControlCmd extends Protocl {

    /**
     * 是否为心跳回复
     *
     * @param cmd
     * @return
     */
    public static boolean isHeartCmd(String cmd) {
        CenterResponse centerResponse = parseCenterResponse(cmd);
        if (centerResponse != null && centerResponse.getCenterMsgID().equals(CMD_RESPONSE_COMMON)) {
            CmdCommonResponse commonResponse = parseCommonResponse(centerResponse.getContent());
            if (commonResponse != null && commonResponse.getMsgID().equals(CMD_SEND_HEART))
                return true;
        }
        return false;
    }

    /**
     * 将协议进行初步解析
     * 1、检查该cmd是否是完整的
     * 2、检查是否达到最短长度要求
     * 3、并不进行校验码校验 但是要计算 留给下边处理
     *
     * @param cmd
     */
    public static CenterResponse parseCenterResponse(String cmd) {
        CenterResponse centerResponse = null;

        int[] cmdFlagIndex = ModuleUtils.getCmdFlagIndex(cmd);
        if (cmdFlagIndex[0] > -1 && cmdFlagIndex[1] > -1 && cmd.length() / 2 > CMD_MIN_SIZE) {
            centerResponse = new CenterResponse();
            cmd = cmd.substring(cmdFlagIndex[0] + FLAG_CMD.length(), cmdFlagIndex[1]);
            cmd = getTransReceiveProtocl2(cmd);

            centerResponse.setCenterMsgID(cmd.substring(0, 4));
            centerResponse.setCenterContentLen(hexIntStrToInt(cmd.substring(4, 8)));
            centerResponse.setCenterDriverPhone(parseDriverPhoneCmd(cmd.substring(8, 20)));
            centerResponse.setCenterSerno(hexIntStrToInt(cmd.substring(20, 24)));

            centerResponse.setContent(cmd.substring(24, cmd.length() - 2));
            centerResponse.setCheckNo(cmd.substring(cmd.length() - 2));

            centerResponse.setCheckNoCalc(ConvertUtils.byteToHexStr(
                    getProtoclCheckCode(ConvertUtils.HexStrToByte(cmd.substring(0, (cmd.length() - 2))))).toUpperCase());
        }
        return centerResponse;
    }


    /**
     * 解析通用回复
     *
     * @param content
     * @return
     */
    public static CmdCommonResponse parseCommonResponse(String content) {
        CmdCommonResponse commonResponse = null;
        if (content.length() >= 10) {
            commonResponse = new CmdCommonResponse();
            commonResponse.setMsgSerno(hexIntStrToInt(content.substring(0, 4)));
            commonResponse.setMsgID(content.substring(4, 8));
            commonResponse.setResult(hexIntStrToInt(content.substring(8)));
        }
        return commonResponse;
    }

    /**
     * 解析下单的命令
     *
     * @param content
     * @return
     */
    public static CmdXDResponse parseXDResponse(String content) {
        CmdXDResponse xdResponse = null;
        if (content.length() > 22) {
            xdResponse = new CmdXDResponse();
            xdResponse.setOrderID(parseOrderSerialNo(content.substring(0, 8)));
            xdResponse.setOrderType(hexIntStrToInt(content.substring(8, 10)));
            xdResponse.setDate(ConvertUtils.bcd2Str(ConvertUtils.HexStrToByte(content.substring(10, 22))));

            List<String> stringList = end00StringToArray(content.substring(22));
            if (stringList != null && stringList.size() > 0) {
                LogUtil.log("handmsg","解析下单的命令字符串:"+stringList.get(0));
                String result=cmdStringstr2String(stringList.get(0));
                LogUtil.log("handmsg","解析下单的命令字符串结果:"+result);
                xdResponse.setOrderAddrDesc(result);
            }

        }
        return xdResponse;
    }

    /**
     * 解析抢单结果的命令
     *
     * @param content
     */
    public static CmdQdResultResponse parseQdResultResponse(String content) {
        CmdQdResultResponse resultResponse = null;
        if (content.length() == 8) {//抢单失败
            resultResponse = new CmdQdResultResponse();
            resultResponse.setQdSuccess(false);
            resultResponse.setOrderID(parseOrderSerialNo(content.substring(0, 8)));
            return resultResponse;
        }
        if (content.length() >= 58) {//抢单成功
            resultResponse = new CmdQdResultResponse();
            resultResponse.setQdSuccess(true);
            resultResponse.setOrderID(parseOrderSerialNo(content.substring(0, 8)));
            resultResponse.setOrderType(hexIntStrToInt(content.substring(8, 10)));
            resultResponse.setUseDate(ConvertUtils.bcd2Str(ConvertUtils.HexStrToByte(content.substring(10, 22))));
            resultResponse.setPassgerLng(uint32ToLatLng(content.substring(22, 30)));
            resultResponse.setPassgerLat(uint32ToLatLng(content.substring(30, 38)));
            resultResponse.setEndLng(uint32ToLatLng(content.substring(38, 46)));
            resultResponse.setEndLat(uint32ToLatLng(content.substring(46, 54)));
            resultResponse.setTip(ConvertUtils.bcd2Str(ConvertUtils.HexStrToByte(content.substring(54, 58))));

            List<String> stringList = end00StringToArray(content.substring(58));
            if (stringList != null && stringList.size() > 0) {
                resultResponse.setPassgerPhone(cmdStringstr2String(stringList.get(0)));
                if (stringList.size() > 1)
                    resultResponse.setOrderDesc(cmdStringstr2String(stringList.get(1)));
            }
        }
        return resultResponse;
    }

    /**
     * 中心下发驾驶员已确认去接乘客
     *
     * @param content
     */
    public static CmdQrQjCkResponse parseQrQjCkResponse(String content) {
        CmdQrQjCkResponse response = null;

        if (content.length() == 8) {
            response = new CmdQrQjCkResponse();
            response.setOrderSno(parseOrderSerialNo(content.substring(0)));
        }
        return response;
    }

    /**
     * 中心下发驾驶员已确认乘客已上车
     *
     * @param content
     */
    public static CmdQrCkScResponse parseQrCkScResponse(String content) {
        CmdQrCkScResponse response = null;

        if (content.length() == 8) {
            response = new CmdQrCkScResponse();
            response.setOrderSno(parseOrderSerialNo(content.substring(0)));
        }
        return response;
    }

    /**
     * 中心取消订单
     *
     * @param content
     */
    public static CmdZxQxDdResponse parseZxQxDdResponse(String content) {
        CmdZxQxDdResponse response = null;

        if (content.length() == 8) {
            response = new CmdZxQxDdResponse();
            response.setOrderSno(parseOrderSerialNo(content.substring(0)));
        }
        return response;
    }

    /**
     * 中心下发车费信息
     *
     * @param content
     */
    public static CmdCostResponse parseCostResponse(String content) {
        CmdCostResponse costResponse = null;

        if (content.length() >= 18) {
            costResponse = new CmdCostResponse();
            costResponse.setOrderID(parseOrderSerialNo(content.substring(0, 8)));
            costResponse.setUseTime(ConvertUtils.bcd2Str(ConvertUtils.HexStrToByte(content.substring(8, 12))));
            costResponse.setTotalDistance(ConvertUtils.bcd2Str(ConvertUtils.HexStrToByte(content.substring(12, 18))));

            List<String> stringList = end00StringToArray(content.substring(18));
            if (stringList != null && stringList.size() > 0) {
                costResponse.setCost(cmdStringstr2String(stringList.get(0)));
                if (stringList.size() > 1)
                    costResponse.setTip(cmdStringstr2String(stringList.get(1)));
            }
        }
        return costResponse;
    }

    /**
     * 中心下发乘客支付车费信息
     *
     * @param content
     */
    public static CmdPasPayResultResponse parsePayResultResponse(String content) {
        CmdPasPayResultResponse response = null;

        if (content.length() >= 10) {
            response = new CmdPasPayResultResponse();
            response.setOrderID(parseOrderSerialNo(content.substring(0, 8)));
            response.setPayResult(hexIntStrToInt(content.substring(8, 10)));

            List<String> stringList = end00StringToArray(content.substring(10));
            if (stringList != null && stringList.size() > 0)
                response.setDesc(cmdStringstr2String(stringList.get(0)));
        }

        return response;
    }

    /**
     * 解析强制司机下班
     *
     * @param content
     */
    public static CmdQzXbResponse parseQzSjXb(String content) {
        CmdQzXbResponse response = null;

        List<String> stringList = end00StringToArray(content);
        if (stringList != null && stringList.size() > 0) {
            response = new CmdQzXbResponse();
            response.setReasonDesc(cmdStringstr2String(stringList.get(0)));
        }

        return response;
    }

    /**
     * 解析其他设备登陆的命令
     *
     * @param content
     * @return
     */
    public static CmdOtherDutyOnResponse parseOtherDutyOn(String content) {
        CmdOtherDutyOnResponse response = null;
        if (content.length() > 2) {
            response = new CmdOtherDutyOnResponse();
            String type = content.substring(0, 2);
            response.setType(type);
            List<String> stringList = end00StringToArray(content);
            if (stringList != null && stringList.size() > 0) {
                response.setDesc(cmdStringstr2String(stringList.get(0)));
            }
        }
        return response;
    }
}
